<template>
  <div>
    <van-nav-bar :title="title" :left-arrow="false" />
    <van-cell title="登记信息" size="large" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.name"
        name="name"
        label="名称"
        placeholder="商家名称"
        :rules="[{ required: true, message: '请填写商家名称' }]"
      />
      <van-field
        v-model="form.address"
        name="address"
        label="地址"
        placeholder="商家地址"
        :rules="[{ required: true, message: '请填写商家地址' }]"
      />
      <van-field
        v-model="form.telephone"
        name="telephone"
        type="tel"
        label="手机"
        placeholder="联系电话"
        :rules="[{ required: true, message: '请填写联系电话' }]"
      />

      <div style="margin: 60px" v-if="flag == 0">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import {
  addWxRetailer,
  getWxRetailerInfo,
  updateWxRetailer,
} from "@/api/member";
export default {
  name: "Retailer",
  data() {
    return {
      form: {
        name: null,
        code: null,
        address: null,
        person: null,
        telephone: null,
        flag: 0,
      },
      flag: 0,
      title: this.$route.meta.title,
    };
  },

  methods: {
    getRetailerInfo() {
      const params = {
        confirm: 1,
      };
      getWxRetailerInfo(params).then((res) => {
        if (res.data.status_code == 200) {
          this.form = res.data.retailer;
          this.flag = 1;
        }
      });
    },
    onSubmit(values) {
      const openid = localStorage.getItem("openid");

      values["openid"] = openid;
      values["id"] = this.form.id;
      this.saveRetailer(values);
    },
    saveRetailer(data) {
      const id = data["id"];
      if (id > 0) {
        updateWxRetailer(data)
          .then((res) => {
            console.log(res.data);
            this.$toast("提交成功!");
          })
          .catch((error) => {
            this.$toast("提交失败!" + error);
          });
      } else {
        addWxRetailer(data)
          .then((res) => {
            console.log(res.data);
            this.form = res.data;
            this.$toast("添加成功!");
          })
          .catch((error) => {
            console.log(error);
            this.$notify({
              type: "danger",
              message: "添加失败",
            });
          });
      }
    },
  },

  created() {
    this.getRetailerInfo();
  },
};
</script>

<style  scoped>
</style>